library _;

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/Const/IFinal.dart';
import 'package:flutter_app/Model/Enums.dart';
import 'package:flutter_app/Util/App/AppUtils.dart';
import 'package:flutter_yfree/YFree.dart';

part 'controller.dart';

part 'view.dart';

class SearchPage extends YPage {
  var textController = TextEditingController();

  List<String> models = [];

  @override
  void yOnCreate() {
    yRead(IFinal.Saves.historySearchs).then((historySearchs) {
      setState(() {
        models = historySearchs == null ? [] : (json.decode(historySearchs) as List<dynamic>).map((e) => e.toString()).toList();
      });
    });
    textController.addListener(() => setState(() {}));
  }

  @override
  void yOnDestory() {
    textController.dispose();
  }

  @override
  Widget yBuild(BuildContext context) {
    return Column(
      children: [
        Container(
          width: yScreenSize().width,
          height: yStatubarHeight() + yNavBarHeight(),
          decoration: BoxDecoration(image: DecorationImage(image: AssetImage(YConfig.navBar), fit: BoxFit.fill)),
          child: Padding(
            padding: EdgeInsets.only(top: yStatubarHeight()),
            child: view_searchBar(),
          ),
        ),
        models.isEmpty
            ? YEmpty()
            : Padding(
                padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                child: Row(
                  children: [
                    Text("历史搜索", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)),
                    YExpanded(),
                    YInkRoundButton(
                      Icon(Icons.delete_forever_outlined, color: Colors.grey, size: 25),
                      size: 50,
                      onTap: () => controller_clearHistory(),
                    ),
                  ],
                ),
              ),
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
            child: Container(
              width: yScreenSize().width,
              child: Wrap(
                spacing: 10,
                runSpacing: 10,
                children: models
                    .map(
                      (model) => YButton(
                        isInk: true,
                        padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                        borderRadius: BorderRadius.circular(99),
                        decoration: BoxDecoration(color: Colors.grey.shade200),
                        child: Text(model, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.grey.shade600)),
                        onTap: () {
                          textController.text = model;
                          controller_search();
                        },
                      ),
                    )
                    .toList(),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
